namespace :hellaworld do
  desc "Updates medias of all categories"
  task(:update_medias_categories => :environment) do
	for categ in Category.rss_feeded_categories
		puts "Updating #{categ.label}"
		categ.update_rss
	end
  end

  desc "Update medias' scores"
  task(:update_medias_scores => :environment) do
    today = Time.now
    medias = Media.find(:all, :conditions => "local_path != \"\"", :order => "updated_at DESC")
    Media.record_timestamps = false
    medias.each do |media|
      age = ((today-media.updated_at)/86400).to_i
      dl_cnt = media.dl_count || 0
      new_score = ( ((dl_cnt+1)/(age+1)) + 30/(age+1)).to_i
      Media.update(media.id, :score => new_score, :updated_at => media.updated_at)
      puts "Updating #{media.nom}(#{media.id}) with score #{new_score}"
    end
    Media.record_timestamps = true
  end
end
